// ---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "statthread.h"
#include "vdskapi.h"
#include "main.h"
#include <stdio.h>
#include <vector>
using namespace std;
#pragma package(smart_init)

extern HANDLE hdisk;
extern TForm1 *Form1;
extern ULONG totcachsize;
extern vector<CACHE> cache;
// ---------------------------------------------------------------------------

// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(&UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall statthread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
// ---------------------------------------------------------------------------

__fastcall statthread::statthread(bool CreateSuspended):TThread(CreateSuspended)
{
}

void __fastcall statthread::Do(void)
{
    if(!hdisk)
        return;

    VDSK_DISK_INFORMATION di;
    VdskQueryDisk(hdisk, &di);

    char str[1024];
    sprintf(str, "%lu", (ULONG)di.IoReadCount.QuadPart);
    Form1->reads->Text = str;
    sprintf(str, "%lu", (ULONG)di.IoWriteCount.QuadPart);
    Form1->writes->Text = str;
    sprintf(str, "%lu", (ULONG)di.IoReadFailed.QuadPart);
    Form1->rfails->Text = str;
    sprintf(str, "%lu", (ULONG)di.IoWriteFailed.QuadPart);
    Form1->wfails->Text = str;

    sprintf(str, "%lu bytes (%d MB)", (ULONG) totcachsize,
        (ULONG) totcachsize/1024/1024);
    Form1->cached->Text = str;

    int unsaved=0;
    for(vector<CACHE>::iterator it=cache.begin();it!=cache.end();it++)
        if((*it).unsaved)
            unsaved++;

        sprintf(str, "%lu blocks, %d unsaved", (ULONG) cache.size(), unsaved);
    Form1->cacheblocks->Text = str;
}

// ---------------------------------------------------------------------------
void __fastcall statthread::Execute()
{
    NameThreadForDebugging("statthread");
    // ---- Place thread code here ----

    while (1)
    {
        Sleep(100);
        Synchronize(Do);
        Application->ProcessMessages();
    }
}
// ---------------------------------------------------------------------------
